import 'package:flutter_admin/pages/common/cry/model/page_model.dart';
import 'package:flutter_admin/pages/common/cry/model/response_body_api.dart';
import 'package:flutter_admin/utils/kwargs.dart';
import 'package:flutter_admin/utils/odoo_http_util.dart';

class UdtTbWorkreportrecordOdooApi {
  static Future<ResponseBodyApi> page({data}) async {
    ResponseBodyApi responseBodyApi;
    try {
      PageModel pageModel = data['page'];
      List domainList = data['domainList'];
      var fieldsInfo = {
        'product.template': [
          'product_id',
          'default_code',
          'name',
          'description'
        ], //需要详细信息的model实体和字段,将获取product_code和product_name
      };
      Kwargs recordsKwargs = Kwargs(
          domain: domainList,
          limit: pageModel.size,
          offset: (pageModel.current - 1) * pageModel.size,
          order: pageModel.orders[0].column! +
              " " +
              (pageModel.orders[0].asc! ? "asc" : "desc"));
      var records = await OdooHttpUtil.callKw(
          model: 'product.workreportrecord',
          method: 'search_read',
          kwargs: recordsKwargs.toMap(),
          fieldsInfo: fieldsInfo,
          filter: data["filter"]);
      var total = await OdooHttpUtil.callKw(
          model: 'product.workreportrecord',
          method: 'search_count',
          kwargs: {"domain": domainList},
          filter: data["filter"]);
      List<Map<String, dynamic>> list = [];
      records.forEach((element) {
        Map<String, dynamic> map = element;
        list.add(map);
      });
      pageModel.records = list;
      pageModel.total = total;
      pageModel.pages = total ~/ pageModel.size + 1;
      responseBodyApi = ResponseBodyApi(success: true, data: pageModel);
    } catch (e) {
      responseBodyApi =
          ResponseBodyApi(success: false, message: '请求出错了：' + e.toString());
    }
    return responseBodyApi;
  }

  static Future<ResponseBodyApi> productPage({data}) async {
    ResponseBodyApi responseBodyApi;
    try {
      PageModel pageModel = data['page'];
      List domainList = data['domainList'];
      Kwargs recordsKwargs = Kwargs(
          domain: domainList,
          limit: pageModel.size,
          offset: (pageModel.current - 1) * pageModel.size,
          order: pageModel.orders[0].column! +
              " " +
              (pageModel.orders[0].asc! ? "asc" : "desc"));
      var records = await OdooHttpUtil.callKw(
          model: 'product.template',
          method: 'product_template_search',
          kwargs: recordsKwargs.toMap(),
      );
      var total = await OdooHttpUtil.callKw(
          model: 'product.template',
          method: 'product_template_search_count',
          kwargs: {"domain": domainList},
      );
      List<Map<String, dynamic>> list = [];
      records.forEach((element) {
        Map<String, dynamic> map = element;
        list.add(map);
      });
      pageModel.records = list;
      pageModel.total = total;
      pageModel.pages = total ~/ pageModel.size + 1;
      responseBodyApi = ResponseBodyApi(success: true, data: pageModel);
    } catch (e) {
      responseBodyApi =
          ResponseBodyApi(success: false, message: '请求出错了：' + e.toString());
    }
    return responseBodyApi;
  }

  static Future<ResponseBodyApi> findAll({data}) async {
    ResponseBodyApi responseBodyApi;
    try {
      List domainList = data['domainList'];
      var fieldsInfo = {
        'product.template': [
          'product_id',
          'default_code',
          'name',
          'description'
        ], //需要详细信息的model实体和字段,将获取product_code和product_name
      };
      Kwargs recordsKwargs = Kwargs(
          domain: domainList,
          );
      var records = await OdooHttpUtil.callKw(
          model: 'product.workreportrecord',
          method: 'search_read',
          kwargs: recordsKwargs.toMap(),
          fieldsInfo: fieldsInfo,
          filter: data["filter"]);
      responseBodyApi = ResponseBodyApi(success: true, data: records);
    } catch (e) {
      responseBodyApi =
          ResponseBodyApi(success: false, message: '请求出错了：' + e.toString());
    }
    return responseBodyApi;
  }

  static Future<ResponseBodyApi> findScreen({data}) async {
    ResponseBodyApi responseBodyApi;
    try {
      Kwargs recordsKwargs = Kwargs(
        domain: [],
      );
      var records = await OdooHttpUtil.callKw(
          model: 'product.workreportrecord',
          method: 'station_report_output_statistics',
          kwargs: recordsKwargs.toMap(),
      );
      responseBodyApi = ResponseBodyApi(success: true, data: records);
    } catch (e) {
      responseBodyApi =
          ResponseBodyApi(success: false, message: '请求出错了：' + e.toString());
    }
    return responseBodyApi;
  }

  static Future<ResponseBodyApi> plantProductionStation({data}) async {
    ResponseBodyApi responseBodyApi;
    try {
      Kwargs recordsKwargs = Kwargs(
        domain: [],
      );
      var records = await OdooHttpUtil.callKw(
          model: 'product.workreportrecord',
          method: 'plant_production_station',
          kwargs: recordsKwargs.toMap(),
      );
      responseBodyApi = ResponseBodyApi(success: true, data: records);
    } catch (e) {
      responseBodyApi =
          ResponseBodyApi(success: false, message: '请求出错了：' + e.toString());
    }
    return responseBodyApi;
  }



  static Future<ResponseBodyApi> changeStatus(data) async {
    ResponseBodyApi responseBodyApi;
    try{
      var result = await OdooHttpUtil.callKw(model: 'product.workreportrecord',method: "change_status", kwargs:{
      'domain':[data]
      },);
      responseBodyApi = ResponseBodyApi(success: true,data: result);
    }catch(e){
      responseBodyApi = ResponseBodyApi(success: false, message: '请求出错了：' + e.toString());
    }
    return responseBodyApi;
  }

  static Future<ResponseBodyApi> removeByIds(data) async {
    ResponseBodyApi responseBodyApi;
    try {
      await OdooHttpUtil.callKw(
          model: 'product.workreportrecord', method: "unlink", args: [data]);
      responseBodyApi = ResponseBodyApi(success: true);
    } catch (e) {
      responseBodyApi =
          ResponseBodyApi(success: false, message: '请求出错了：' + e.toString());
    }
    return responseBodyApi;
  }

  static Future<ResponseBodyApi> saveOrUpdate(data) async {
    ResponseBodyApi responseBodyApi;
    print(data);
    try {
      if (data["id"] == null) {
        await OdooHttpUtil.callKw(
            model: 'product.workreportrecord', method: 'create', args: [data]);
      } else {
        await OdooHttpUtil.callKw(
            model: 'product.workreportrecord',
            method: 'write',
            args: [
              [data["id"]],
              data
            ]);
      }
      responseBodyApi = ResponseBodyApi(success: true);
    } catch (e) {
      responseBodyApi =
          ResponseBodyApi(success: false, message: '请求出错了：' + e.toString());
    }
    return responseBodyApi;
  }

  static Future<ResponseBodyApi> batchSave(List data) async {
    ResponseBodyApi responseBodyApi;
    try {
      await OdooHttpUtil.callKw(
          model: 'product.workreportrecord', method: 'create', args: [data]);
      responseBodyApi = ResponseBodyApi(success: true);
    } catch (e) {
      responseBodyApi =
          ResponseBodyApi(success: false, message: '请求出错了：' + e.toString());
    }
    return responseBodyApi;
  }

}
